package com.example.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.hz.MOCTA;
import com.example.entity.vo.MoctaQueryVo;
import com.example.entity.vo.SfcteQueryVo;
import com.example.entity.vo.WorkOrderOvertimeVo;
import com.example.entity.vo.WorkOrderVo;
import com.example.entity.vo.pc.ApsGDList;
import com.example.entity.vo.pc.PcMoctaList;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author LiangPandeng
 * @since 2024-02-28
 */
public interface MoctaMapper extends BaseMapper<MOCTA> {
    void modifyMOCTA(@Param("TY001")String TY001 , @Param("TY002")String TY002);

    List<PcMoctaList> getPcMoctaList(@Param("orderSql") String orderSql);

    /**
     * 根据 工单单别 单号 获取工单单头详情
     */
    @DS("demo")
    @Select("select  * from MOCTA where TA001 = #{TA001} and TA002 = #{TA002}")
    MOCTA getByTA001WithTA002(String TA001, String TA002);

    @DS("demo")
    @Select("SELECT COUNT(*) FROM " +
            "MOCTA LEFT JOIN CMSMD " +
            "ON TA021=MD001 LEFT JOIN PURMA " +
            "ON MA001=TA032 " +
            "WHERE TA011<>'Y' " +
            "AND TA011<>'y' " +
            "AND TA013='Y' " +
            "AND TA010<CONVERT(CHAR(8),GETDATE(),112)")
    Integer getOverdueNum();

    @DS("demo")
    @Select("SELECT ISNULL(ISNULL(MD002,MA002),'') workCenter,TA003 beginDate,TA001 workSeparate,TA002 workNumber,RTRIM(TA006) productArticle,TA034 productName,TA035 productSpecification,CAST(TA015 AS REAL) expectedOutput,CAST(TA017 AS REAL) storedItems,TA009 expectedStart,TA010 expectedEnd " +
            "FROM MOCTA LEFT JOIN CMSMD " +
            "ON TA021=MD001 LEFT JOIN PURMA " +
            "ON MA001=TA032 " +
            "WHERE TA011<>'Y' " +
            "AND TA011<>'y' " +
            "AND TA013='Y' " +
            "AND TA010<CONVERT(CHAR(8),GETDATE(),112) " +
            "ORDER BY ISNULL(ISNULL(MD002,MA002),'') desc")
    List<WorkOrderVo> getOverdueDetails();

    @DS("demo")
    @Select("SELECT ISNULL(ISNULL(MD002,MA002),'') workCenter,COUNT(*) 'number'" +
            "FROM MOCTA LEFT JOIN CMSMD " +
            "ON TA021=MD001 LEFT JOIN PURMA " +
            "ON MA001=TA032 " +
            "WHERE TA011<>'Y' " +
            "AND TA011<>'y' " +
            "AND TA013='Y' " +
            "AND TA010<CONVERT(CHAR(8),GETDATE(),112) " +
            "GROUP BY ISNULL(ISNULL(MD002,MA002),'') " +
            "ORDER BY ISNULL(ISNULL(MD002,MA002),'') desc")
    List<WorkOrderVo> getOverdueInfo();

    @DS("demo")
    @Select("SELECT ISNULL(ISNULL(m.MD002, ma.MA002),'') workCenter,TA001 single,TA002 singleNumber,TA003 beginDate, TA001 workSeparate, TA002 workNumber, RTRIM(TA006) productArticle, TA034 productName, TA035 productSpecification, CAST(TA015 AS REAL) expectedOutput, CAST(TA017 AS REAL) storedItems, TA009 expectedStart, TA010 expectedEnd " +
            "FROM MOCTA t " +
            "LEFT JOIN CMSMD m ON t.TA021 = m.MD001 " +
            "LEFT JOIN PURMA ma ON ma.MA001 = t.TA032 " +
            "WHERE t.TA011<>'Y' " +
            "AND t.TA011<>'y' " +
            "AND t.TA013='Y' " +
            "AND t.TA010<CONVERT(CHAR(8),GETDATE(),112) " +
            "AND ISNULL(ISNULL(m.MD002, ma.MA002),'') = #{workCenter} " +
            "ORDER BY t.TA009")
    IPage<WorkOrderVo> getOverdueDetailsByWorkCenter(Page<WorkOrderVo> page, @Param("workCenter") String workCenter);

    @DS("demo")
    @Select("SELECT COUNT(*) 'number'" +
            "FROM MOCTA LEFT JOIN CMSMD " +
            "ON TA021=MD001 LEFT JOIN PURMA " +
            "ON MA001=TA032 " +
            "WHERE TA011<>'Y' " +
            "AND TA011<>'y' " +
            "AND TA013='Y' " +
            "AND TA010<CONVERT(CHAR(8),GETDATE(),112) " +
            "GROUP BY ISNULL(ISNULL(MD002,MA002),'') " +
            "HAVING ISNULL(ISNULL(MD002,MA002),'') = #{workCenter} " +
            "ORDER BY ISNULL(ISNULL(MD002,MA002),'') desc " )
    String getOverdueCount(String workCenter);

    @DS("demo")
    List<WorkOrderOvertimeVo> getOverdueDetailsInfo(String workCenter, String single, String singleNumber);

    IPage<MOCTA> selectAll(Page<MOCTA> page);

    List<MoctaQueryVo> queryMoctaQueryVo(MoctaQueryVo moctaQueryVo);

    List<SfcteQueryVo> querySfcte(SfcteQueryVo sfcteQueryVo);

    List<String> getmoctaKeyList();

    List<ApsGDList> gdlb();

    List<String> getmocta2KeyList();

    List<MOCTA> getMoctaFromMocty(String date);
}
